﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.MemoryMappedFiles;
using System.IO;

namespace InCom.Core.TerminalSend.MappedFile
{
    /// <summary>
    /// 文件共享
    /// </summary>
    public class MemoryMappedFileOperater
    {
        private static MemoryMappedFileOperater instance = null;

        /// <summary>
        ///  提供文件共享实例
        /// </summary>
        public static MemoryMappedFileOperater Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new MemoryMappedFileOperater();
                    instance.CreateOrGetFile();
                }
                return instance;
            }
        }



        /// <summary>
        /// 共享文件
        /// </summary>
        public string FineName { get; protected set; }
        /// <summary>
        /// 文件大小 默认20MB
        /// </summary>
        private long capacity = 20 * 1024 * 1024;
        /// <summary>
        /// 文件大小 默认20MB
        /// </summary>
        public long Capacity
        {
            get { return capacity; }
            protected set { capacity = value; }
        }
        /// <summary>
        /// 共享文件
        /// </summary>
        public System.IO.MemoryMappedFiles.MemoryMappedFile File { get; protected set; }

        /// <summary>
        /// 内存文件视图
        /// </summary>
        protected System.IO.MemoryMappedFiles.MemoryMappedViewAccessor accessorView = null;
        /// <summary>
        /// 内存文件视图
        /// </summary>
        public System.IO.MemoryMappedFiles.MemoryMappedViewAccessor AccessorView
        {
            get { return accessorView; }
        }
        /// <summary>
        /// 创建文件共享,如果文件存在则读取共享文件
        /// </summary>
        protected  virtual void CreateOrGetFile()
        {
            FineName = "共享文件";
            //基于内存创建
            System.IO.MemoryMappedFiles.MemoryMappedFile file = System.IO.MemoryMappedFiles.MemoryMappedFile.CreateOrOpen(FineName,capacity, System.IO.MemoryMappedFiles.MemoryMappedFileAccess.ReadWrite);
            File = file;
            accessorView = file.CreateViewAccessor();
           
        }

        /// <summary>
        /// 读取字节
        /// </summary>
        /// <param name="offset">从0 开始的数据依稀量</param>
        /// <param name="length">要读取的数据长度</param>
        /// <returns></returns>
        public byte[] Read(int offset, int length)
        {
            int n = 0;
            byte[] buff = new byte[length];
            //循环读取
            for (int k = offset; k < offset + length; k++)
            {
                buff[n] = accessorView.ReadByte(k);
                n++;
            }
            return buff;
        }


        /// <summary>
        /// 写入一个字节数组
        /// </summary>
        /// <param name="bytes">要写入的文件数组</param>
        /// <param name="writeoffset">要写入文件起始位置</param>
        /// <returns>返回写入后的数据偏移量</returns>
        public int Write(byte[] bytes, int writeoffset)
        {
            int offset = writeoffset;
            //循环写入
            for (int i = 0; i < bytes.Length; i++)
            {
                accessorView.Write(offset, bytes[i]);
                offset++;
            }
            return offset;
        }





    }
}
